package cn.stylefeng.guns.modular.basic.controller;

import java.util.Date;
import java.util.List;

import cn.stylefeng.guns.sys.modular.system.entity.SysCompany;
import cn.stylefeng.guns.sys.modular.system.model.params.SysCompanyParam;
import cn.stylefeng.guns.sys.modular.system.model.result.SysCompanyResult;
import cn.stylefeng.guns.sys.modular.system.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 运营部表（原系统：分公司表）控制器
 *
 * @author zhucx
 * @Date 2019-06-21 16:19:59
 */
@Controller
@RequestMapping("/basicOperate")
public class BasicOperateController extends BaseController {

    private String PREFIX = "/basic/basicOperate";

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicOperate.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        SysCompanyParam sysCompanyParam = new SysCompanyParam();
        sysCompanyParam.setDeliverId(user.getDeliverId());
        List<SysCompanyResult> ztlist = sysCompanyService.list2(sysCompanyParam);
        model.addAttribute("ztlist",ztlist);
        return PREFIX + "/basicOperate_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @Permission
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        SysCompanyParam sysCompanyParam = new SysCompanyParam();
        sysCompanyParam.setDeliverId(user.getDeliverId());
        List<SysCompanyResult> ztlist = sysCompanyService.list2(sysCompanyParam);
        model.addAttribute("ztlist",ztlist);
        return PREFIX + "/basicOperate_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicOperateParam basicOperateParam) {
        SysCompany sysCompany = sysCompanyService.getById(basicOperateParam.getCoId());
        basicOperateParam.setCoName(sysCompany.getCoName());
        //set 交付id
    	Long DeliverId = userService.getDeliverId();
    	if (DeliverId!=null) {
    		basicOperateParam.setDeliverId(DeliverId);
		}
    	//添加部门
        Dept dept = new Dept();
    	dept.setPid(basicOperateParam.getDeptId());
    	dept.setSimpleName(basicOperateParam.getOperateName());
    	dept.setFullName(basicOperateParam.getOperateName());
    	dept.setDeliverId(DeliverId);
    	deptService.addDept(dept);
    	
    	Dept d = deptService.findBySpec(dept);
    	basicOperateParam.setDeptId(d.getDeptId());
    	basicOperateParam.setDeptName(d.getFullName());
    	basicOperateParam.setStatus("1");
    	basicOperateParam.setCreateTime(new Date());
        this.basicOperateService.addReturnId(basicOperateParam);
        
        d.setOperateId(Long.parseLong(basicOperateParam.getId().toString()));
        deptService.editDept(d);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicOperateParam basicOperateParam) {
    	Object coId = basicOperateParam.getCoId();
    	if (coId!=null) {
            SysCompany sysCompany = sysCompanyService.getById(basicOperateParam.getCoId());
            basicOperateParam.setCoName(sysCompany.getCoName());
		}
        this.basicOperateService.update(basicOperateParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicOperateParam basicOperateParam) {
        this.basicOperateService.delete(basicOperateParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicOperateParam basicOperateParam) {
        BasicOperate detail = this.basicOperateService.getById(basicOperateParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicOperateParam basicOperateParam,String condition) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setDeptId(user.getDeptId());
        return this.basicOperateService.findPageBySpec(basicOperateParam,condition,user.getOperateId());
    }

}


